package com.wms.web.action.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wms.data.domain.service.MenuService;
import com.wms.data.pojo.Menu;
import com.wms.web.action.AbstractAction;

@ParentPackage("wms")
@Namespace("/app")
@Result(name="exception", location="/WEB-INF/jsp/common/error.jsp")
@ExceptionMappings(@ExceptionMapping(exception="java.lang.Exception", result="exception"))
public class MenuAction extends AbstractAction {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private MenuService menuService;
	
	private Menu menu;
	
	private String menuId;
	
	private List<Menu> lstMainMenu = new ArrayList<Menu>();
	
	/**
	 * Menu list.
	 *
	 * <pre>
	 * 	init Login
	 * </pre>
	 *
	 * @return String
	 */
	@Action(value="menuList", results={@Result(name="success", location="/WEB-INF/jsp/system/menu/menu_list.jsp")})
	public String initMenuList() {
		
		return SUCCESS;
	}
	
	@Action(value="menuNew", results={@Result(name="success", location="/WEB-INF/jsp/system/menu/menu_edit.jsp")})
	public String initNew() {
		
		setLstMainMenu(menuService.getMainMenu());
		
		return SUCCESS;
	}
	
	@Action(value="menuEdit", results={@Result(name="success", location="/WEB-INF/jsp/system/menu/menu_edit.jsp")})
	public String initEdit() {
		
		setLstMainMenu(menuService.getMainMenu());
		
		Menu menu = menuService.findById(getMenuId());
		
		setMenu(menu);
		
		return SUCCESS;
	}
	
	@Action(value="menuSave", results={@Result(name="success", location="/WEB-INF/jsp/system/menu/menu_edit.jsp")})
	public String save() {
		
		menuService.saveMenu(getMenu());
		
		return SUCCESS;
	}

	public List<Menu> getLstMainMenu() {
		return lstMainMenu;
	}

	public void setLstMainMenu(List<Menu> lstMainMenu) {
		this.lstMainMenu = lstMainMenu;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}
}